var param = {
	companyId: $.Cookie("companyId"),
	userId: $.Cookie("userId"),
	session: $.Cookie("session")
};
$.ajax({
	url: 'dvcGroupList',
	type: 'POST',
	data: param,
	dataType: 'json',
	success: function(data) {
		if(data.errorCode == 0) {
			$.ajax({
				url: 'orgListTree',
				type: 'POST',
				data: param,
				dataType: 'json',
				success: function(data) {
					if(data.errorCode == 0) {
						var setting = {
							check: {
								enable: true,
								autoCheckTrigger: true,
								chkboxType: {
									Y: "",
									N: ""
								}
							},
							data: {
								simpleData: {
									enable: true
								}
							},
							callback: {
								onCheck: onCheck
							}
						};

						var zNodes = data.result;
						var code, log, className = "dark";
						var somearTree = [];

						function removeByValue(arr, val) {
							for(var i = 0; i < arr.length; i++) {
								if(arr[i] == val) {
									arr.splice(i, 1);
									break;
								}
							}
						}

						function onCheck(e, treeId, treeNode) {

							if(treeNode.checked == true) {
								removeByValue(somearTree, treeNode.name);
								somearTree.push(treeNode.name);
								$(".manageAdd_content_d_content_input_a").attr("value", somearTree)
							} else {
								removeByValue(somearTree, treeNode.name);

							}
						}

						function showCode(str) {
							if(!code) code = $("#code");
							code.empty();
							code.append("<li>" + str + "</li>");
						}

						$(document).ready(function() {
							$.fn.zTree.init($("#treeDemo"), setting, zNodes);

						});

					} else if(data.errorCode == -6) {
						alert("登录超时！")
						top.location.href = 'index.jsp';
					}
				},
				error: function(err) {
					alert("连接服务器失败！请重新提交！")
					top.location.href = 'index.jsp';

				}
			});
			$.ajax({
				url: 'paramTypeList',
				type: 'POST',
				data: {
					session: $.Cookie("session"),
					userId: $.Cookie("userId"),
					companyId: $.Cookie("companyId"),
					index: 0,
					page: 20
				},
				dataType: 'json',
				success: function(data) {
					if(data.errorCode == 0) {
						$.each(data.result.paramTypeList, function(i, value) {
							if(data.result.paramTypeList[i].type == "ORG_TYPE") {} else {
								error_tpl = '<input class="manageAdd_content_b_label_a" id="chenck_' + data.result.paramTypeList[i].id + '" type="checkbox" name="' + data.result.paramTypeList[i].paramValue + '" value="' + data.result.paramTypeList[i].id + '" onclick="deviceType(' + "this" + ')" > <div>' + data.result.paramTypeList[i].paramValue + '</div>'
								$(".deviceType").append(error_tpl)
							}
						});
					} else if(data.errorCode == -6) {
						alert("登录超时！")
						top.location.href = 'index.jsp';
					} else {
						alert("请重试！")
					}
				},
				error: function(err) {
					alert("连接服务器失败！请重新提交！")
					top.location.href = 'index.jsp';
				}
			});
			sessionStorage.setItem("GroupAll", data.result.length);
			$.each(data.result, function(i, value) {
				var error_tpl = '<div style="height: 20px;float: left;width: 100%;"><input type="checkbox" id="' + i + '" name="' + value.dvcGroupName + '" value="' + value.dvcGroupId + '" onclick="lista(this)"/><span>' + value.dvcGroupName + '</span></div>';
				$(".manageAdd_content_d_content_input_div_b").append(error_tpl)
			});

		} else if(data.errorCode == -6) {
			alert("登录超时！")
			top.location.href = 'index.jsp';

		}

	},
	error: function(err) {
		alert("连接服务器失败！请重新提交！")
		top.location.href = 'index.jsp';

	}
});

$("#time_begin").timepicki();
$("#time_finish").timepicki();
var apkFileId;
var dvcRule = 0;
var downloadTimeRule = 0;
var downloadRule = 0;
var updateRule = 0;
var recommendRule = 0;

function formAjax() {
	$(".ppp").show()
	$(".spinnera").show()
	var form = new FormData(document.getElementById("formflie"));
	$.ajax({
		url: 'uploadApk',
		type: "POST",
		data: form,
		processData: false,
		contentType: false,
		success: function(data) {
			if(data.errorCode == 0) {
				$(".appVersion").attr("value", data.result.appVersion);
				$(".appName").attr("value", data.result.appName);
				$(".appSize").attr("value", data.result.appSize);
				$(".appLogoPath").attr("value", data.result.appLogoPath);
				$(".appPackageName").attr("value", data.result.appPackageName);
				$(".appVersionName").attr("value", data.result.appVersionName);
				apkFileId = data.result.apkFileId;
				$(".manageAdd_content_a").hide();
				$(".manageAdd_content_b").show();
				$("#back_Out").hide();
				$("#back,#backOut").show();
				$(".manageAdd_header_img_b").attr('src', 'img/manageAdd_B-1.png');
				$(".ppp").hide()
				$(".spinnera").hide()
			} else if(data.errorCode == -6) {
				alert("登录超时！")
				top.location.href = 'index.jsp';
			}
		},
		error: function(e) {
			alert("连接服务器失败！");
			top.location.href = 'index.jsp';
		}
	});
}

function setAppInfo() {
	var param = {
		developer: document.getElementById("developer").value,
		deviceType: document.getElementById("deviceType").value,
		summaryDesc: document.getElementById("summaryDesc").value,
		detailDesc: document.getElementById("detailDesc").value,
		updateDesc: document.getElementById("updateDesc").value,
		osInfo: document.getElementById("osInfo").value,
		apkFileId: apkFileId,
		userId: $.Cookie("userId"),
		session: $.Cookie("session")
	}
	$.ajax({
		url: 'setAppInfo',
		type: 'POST',
		data: param,
		dataType: 'json',
		success: function(data) {
			if(data.errorCode == 0) {} else if(data.errorCode == -6) {
				alert("登录超时！")
				top.location.href = 'index.jsp';
			}
		},
		error: function(err) {
			alert("连接服务器失败！请重新提交！")
			top.location.href = 'index.jsp';
		}
	});
}

//显示机构
$(".manageAdd_content_d_content_input_a").click(function() {
	$(".manageAdd_content_d_content_input_div").show()
});
//			离开事件
$(".manageAdd_content_d_content_input_div").mouseleave(function() {
	$(".manageAdd_content_d_content_input_div").hide()
});
//			离开事件
$(".manageAdd_content_d_content_input_div_b").mouseleave(function() {
	$(".manageAdd_content_d_content_input_div_b").hide()
});
//显示机构
$(".manageAdd_content_d_content_input_b").click(function() {
	$(".manageAdd_content_d_content_input_div_b").show()
});

//机构
$("#getDevice_frist").click(function() {
	if($(".divice_header_frist").css("display") == 'block') {
		$(".add").addClass("device_add");
		$(".file").addClass("device_file");
		$(".file").removeClass("device-file");
		$(".add").removeClass("device-add");
		$(".divice_header_frist").fadeToggle();
	} else if($(".divice_header_frist").css("display") == 'none') {
		$(".divice_header_frist").slideToggle();
		$(".add").addClass("device-add");
		$(".file").addClass("device-file");
		$(".file").removeClass("device_file");
		$(".add").removeClass("device_add");
	}
});

function loadImage(img) {
	var filePath = img.value;
	var fileExt = filePath.substring(filePath.lastIndexOf("."))
		.toLowerCase();
	if(!checkFileExt(fileExt)) {
		alert("您上传的文件不是APK格式,请重新上传！");
		img.value = "";
		return;
	}

	function checkFileExt(ext) {
		if(!ext.match(/.apk/i)) {
			return false;
		}
		return true;
	}
	if(img.files && img.files[0]) {
		$(".manageAdd_content_a_div").text(img.files[0].name);

	} else {
		img.select();
		var url = document.selection.createRange().text;
		try {
			var fso = new ActiveXObject("Scripting.FileSystemObject");
		} catch(e) {
			alert('如果你用的是ie8以下 请将安全级别调低！');
		}

	}
}

function check(obj) {
	var a = [];
	var b = [];
	for(i = 0; i < document.all(obj).length; i++) {
		if(document.all(obj)[i].checked) {
			b[b.length] = document.all(obj)[i].id;
			if(a == '') {
				a = a + document.all(obj)[i].value;
			} else {
				a = a + ',' + document.all(obj)[i].value;
			}
		}
	}
	$(".manageAdd_content_d_content_input_a").val(a);
}

var daviceTypeId = [];

function deviceType(data) {
	var typeid = document.getElementById(data.id);
	if(typeid.checked == true) {
		removeByValue(daviceTypeId, typeid.name);
		daviceTypeId.push(typeid.name);
	} else if(typeid.checked == false) {
		removeByValue(daviceTypeId, typeid.name);
	}
	$(".input_down").attr("value", daviceTypeId)
}

var somearray = []
var somename = []

function removeByValue(arr, val) {
	for(var i = 0; i < arr.length; i++) {
		if(arr[i] == val) {
			arr.splice(i, 1);
			break;
		}
	}
}

function lista(data) {
	var tickBlock = document.getElementById(data.id);
	if(tickBlock.checked == true) {
		removeByValue(somearray, data.value);
		removeByValue(somename, data.name);
		somearray.push(data.value);
		somename.push(data.name);
	} else if(tickBlock.checked == false) {
		removeByValue(somearray, data.value);
		removeByValue(somename, data.name);
	}
	$(".manageAdd_content_d_content_input_b").attr("value", somename);
}

function loadTexta() {
	var a = $(".manageAdd_c_a").val();
	a = $.trim(a)
	if(a == '') {
		$(".manageAdd_c_a_img").hide();
		$(".manageAdd_c_a_span").show();
	} else if(!$(".manageAdd_c_a").val() == '') {
		$(".manageAdd_c_a_img").show();
		$(".manageAdd_c_a_span").hide();

	}
}

function loadTextb() {
	var b = $(".manageAdd_c_b").val();
	b = $.trim(b)
	if(b == ' ') {
		$(".manageAdd_c_b_img").hide();
		$(".manageAdd_c_b_span").show();
	} else if(!$(".manageAdd_c_b").val() == ' ') {
		$(".manageAdd_c_b_img").show();
		$(".manageAdd_c_b_span").hide();

	}
}

function loadTextc() {
	var c = $(".manageAdd_c_c").val();
	c = $.trim(c)
	if(c == '') {
		$(".manageAdd_c_c_img").hide();
		$(".manageAdd_c_c_span").show();
	} else if(!$(".manageAdd_c_c").val() == '') {
		$(".manageAdd_c_c_img").show();
		$(".manageAdd_c_c_span").hide();

	}
}

function loadTextd() {
	var d = $(".manageAdd_c_d").val();
	d = $.trim(d)
	if(d == '') {
		$(".manageAdd_c_d_img").hide();
		$(".manageAdd_c_d_span").show();
	} else if(!$(".manageAdd_c_d").val() == '') {
		$(".manageAdd_c_d_img").show();
		$(".manageAdd_c_d_span").hide();

	}
}

function loadTexte() {
	var e = $(".manageAdd_c_e").val();
	e = $.trim(e)
	if(e == '') {
		$(".manageAdd_c_e_img").hide();
		$(".manageAdd_c_e_span").show();
	} else if(!$(".manageAdd_c_e").val() == '') {
		$(".manageAdd_c_e_img").show();
		$(".manageAdd_c_e_span").hide();

	}
}

! function() {
	laydate.skin('danlan'); //切换皮肤，请查看skins下面皮肤库
	laydate({
		elem: '#demo'
	}); //绑定元素

}();

//			点击事件
function labelShow() {
	$(".manageAdd_content_b_label").show()
}

$(".input_down,.pull-down,.manageAdd_content_b_x").hover(function() {
	$(".manageAdd_content_b_x").show();

}, function() {
	$(".manageAdd_content_b_x").hide();
});

//			input监听
$('.input_down').bind('input propertychange', function() {
	$(".manageAdd_content_b_label").show()
});
//			离开事件
$(".manageAdd_content_b_label").mouseleave(function() {
	$(".manageAdd_content_b_label").hide()
});

function userDefinedA() {
	if($(".institution_whole_ok").css("display") == 'block') {
		$(".institution_whole_ok").hide();
	} else if($(".institution_whole_ok").css("display") == 'none') {
		$(".institution_whole_ok").show();
		$(".organization_ok").hide();
		dvcRule = 0;
		$(".institution_ok").hide();

	}
}

function userDefinedB() {
	if($(".institution_ok").css("display") == 'block') {
		$(".institution_ok").hide();

	} else if($(".institution_ok").css("display") == 'none') {
		$(".institution_ok").show();
		$(".organization_ok").hide();
		$(".institution_whole_ok").hide();

	}
}

function userDefinedC() {
	if($(".organization_ok").css("display") == 'block') {
		$(".organization_ok").hide();

	} else if($(".organization_ok").css("display") == 'none') {
		$(".organization_ok").show();
		$(".institution_ok").hide();
		$(".institution_whole_ok").hide();

	}

}

function userDefinedD() {
	if($(".time_whole_ok").css("display") == 'block') {
		$(".time_whole_ok").hide();

	} else if($(".time_whole_ok").css("display") == 'none') {
		$(".time_whole_ok").show();
		downloadTimeRule = 0;
		$(".day_ok").hide();
		$(".time_ok").hide();

	}

}

function userDefinedE() {
	if($(".day_ok").css("display") == 'block') {
		$(".day_ok").hide();
	} else if($(".day_ok").css("display") == 'none') {
		$(".day_ok").show();
		$(".time_whole_ok").hide();
		$(".time_ok").hide();
	}

}

function userDefinedF() {
	if($(".time_ok").css("display") == 'block') {
		$(".time_ok").hide();
	} else if($(".time_ok").css("display") == 'none') {
		$(".time_ok").show();
		$(".time_whole_ok").hide();
		$(".day_ok").hide();
	}

}

function userDefinedG() {
	if($(".update_ok_ok").css("display") == 'block') {
		$(".update_ok_ok").hide();
	} else if($(".update_ok_ok").css("display") == 'none') {
		$(".update_ok_ok").show();
		$(".update_no_ok").hide();
		updateRule = 0;

	}

}

function userDefinedH() {
	if($(".update_no_ok").css("display") == 'block') {
		$(".update_no_ok").hide();
	} else if($(".update_no_ok").css("display") == 'none') {
		$(".update_no_ok").show();
		$(".update_ok_ok").hide();
		updateRule = 1;

	}
}

function userDefinedI() {
	if($(".download_ok_ok").css("display") == 'block') {
		$(".download_ok_ok").hide();
	} else if($(".download_ok_ok").css("display") == 'none') {
		$(".download_ok_ok").show();
		$(".download_no_ok").hide();
		downloadRule = 0;
	}
}

function userDefinedL() {
	if($("download_no_ok").css("display") == 'block') {
		$(".download_no_ok").hide();
	} else if($(".download_no_ok").css("display") == 'none') {
		$(".download_no_ok").show();
		$(".download_ok_ok").hide();
		downloadRule = 1;

	}
}

function userDefinedJ() {
	if($(".tabletop_ok_ok").css("display") == 'block') {
		$(".tabletop_ok_ok").hide();
	} else if($(".tabletop_ok_ok").css("display") == 'none') {
		$(".tabletop_ok_ok").show();
		$(".tabletop_no_ok").hide();
		recommendRule = 0;
	}
}

function userDefinedK() {
	if($(".tabletop_no_ok").css("display") == 'block') {
		$(".tabletop_no_ok").hide();
	} else if($(".tabletop_no_ok").css("display") == 'none') {
		$(".tabletop_no_ok").show();
		$(".tabletop_ok_ok").hide();
		recommendRule = 1;

	}
}

function accomplish() {
	var param = {
		appCode: document.getElementById("manageTime").value,
		userId: $.Cookie("userId"),
		session: $.Cookie("session"),
		companyId: $.Cookie("companyId"),
		dvcRule: dvcRule,
		downloadTimeRule: downloadTimeRule,
		downloadRule: downloadRule,
		updateRule: updateRule,
		recommendRule: recommendRule,
	}
	$.ajax({
		url: 'setAppDownloadRule',
		type: 'POST',
		data: param,
		dataType: 'json',
		success: function(data) {
			if(data.errorCode == 0) {
				alert("提交成功")
				window.location.href = 'manage.jsp';
			} else if(data.errorCode == "-1") {
				alert("系统异常请重试")
			} else if(data.errorCode == "-2") {
				alert("没有全部填完！")
			} else if(data.errorCode == "-6") {
				alert("登录超时！")
				top.location.href = 'index.jsp';

			} else {
				alert("系统异常请重试")
			}
		},
		error: function(err) {

			alert("连接服务器失败！请重新提交！");
			top.location.href = 'index.jsp';

		}
	});
}
//			选中塞纳
var mycars = new Array("SN8888", "SW99999", "SWWW88", "WSV8888")

function nextStep() {
	if($(".manageAdd_content_a").css("display") == 'block') {

		if($(".fileAjax").text() == '') {
			alert("请选择上传的文件")
		} else {
			formAjax();

		}

	} else if($(".manageAdd_content_b").css("display") == 'block') {
		$(".manageAdd_content_b").hide();
		$(".manageAdd_content_c").show();
		$(".manageAdd_header_img_c").attr('src', 'img/manageAdd_C-1.png')
	} else if($(".manageAdd_content_c").css("display") == 'block') {
		setAppInfo()
		$(".manageAdd_content_c,#next-step").hide();
		$(".manageAdd_content_d,#accomplish").show();
		$(".manageAdd_header_img_d").attr('src', 'img/manageAdd_D-1.png')
	}

}

function back() {
	if($(".manageAdd_content_b").css("display") == 'block') {
		$(".manageAdd_content_b").hide();
		$(".manageAdd_content_a").show();
		$("#back_Out").show();
		$("#back,#backOut").hide();
		$(".manageAdd_header_img_b").attr('src', 'img/manageAdd_b.png')
	} else if($(".manageAdd_content_c").css("display") == 'block') {
		$(".manageAdd_content_b").show();
		$(".manageAdd_content_c").hide();
		$(".manageAdd_header_img_c").attr('src', 'img/manageAdd_c.png')
	} else if($(".manageAdd_content_d").css("display") == 'block') {
		$(".manageAdd_content_c,#next-step").show();
		$(".manageAdd_content_d,#accomplish").hide();
		$(".manageAdd_header_img_d").attr('src', 'img/manageAdd_d.png')
	}
}

function backOut() {
	window.location.href = 'manage.jsp';

}

$("#mangeeAddAchieve").click(function() {
	var myDate = new Date();
	myDate.getYear(); //获取当前年份(2位)
	var a = myDate.getYear();
	var b = myDate.getMonth() + 1;
	var c = myDate.getDate();
	var d = myDate.getHours();
	var e = myDate.getSeconds(); //获取当前秒数(0-59)
	var f = myDate.getMilliseconds(); //获取当前毫秒数(0-999)
	var g = a + "" + b + "" + c + "" + d + "" + e + "" + f

	$("#manageTime").attr("value", g); //填充内容

	$(".appCode").attr("value", g);
})

function gain() {
	var myDate = new Date();
	myDate.getYear(); //获取当前年份(2位)
	var a = myDate.getYear();
	var b = myDate.getMonth() + 1;
	var c = myDate.getDate();
	var d = myDate.getHours();
	var e = myDate.getSeconds(); //获取当前秒数(0-59)
	var f = myDate.getMilliseconds(); //获取当前毫秒数(0-999)
	var g = a + "" + b + "" + c + "" + d + "" + e + "" + f
	$("#manageTime").attr("value", g); //填充内容
	$(".appCode").attr("value", g);

	$(".companyId").attr("value", $.Cookie("companyId"));
	$(".userId").attr("value", $.Cookie("userId"));
	$(".session").attr("value", $.Cookie("session"));

}
gain()